package com.example.library_common.album.utils;

import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;

import com.example.library_common.R;

import java.util.ArrayList;
import java.util.List;

public class AlbumTakePhotoSound {

    private SoundPool pool;
    private List<Object> listPool = new ArrayList<>();
    private boolean isOpen;//是否播放声音
    public void initPool(Context context) {
        setOpen(true);

        pool = new SoundPool(10, AudioManager.STREAM_SYSTEM, 5);

        listPool.add(pool.load(context, R.raw.take_pic, 0));//蜂鸣器 2
        listPool.add(pool.load(context, R.raw.take_pic, 0));//蜂鸣器 2
        listPool.add(pool.load(context, R.raw.take_pic, 0));//蜂鸣器 2
    }

    private static AlbumTakePhotoSound instance = null;

    private AlbumTakePhotoSound(){
    }

    public static AlbumTakePhotoSound getInstance() {
        synchronized (AlbumTakePhotoSound.class) {
            if (instance == null) {
                instance = new AlbumTakePhotoSound();
            }
        }

        return instance;
    }

    public boolean isOpen() {
        return isOpen;
    }

    public void setOpen(boolean open) {
        isOpen = open;
    }

    /**
     * list 坐标位置
     * @param flag
     */
    public  void playVoice(int flag) {
        boolean b = isOpen();
        if(b){
            // 消费成功 提示声音
            if (null != pool) {
                int index = (Integer) listPool.get(flag);
                pool.play(index, 1, 1, 0, 0, 1);
            }
        }else if(flag == 0 && !b){
            flag = (listPool.size()-1);
            if (null != pool) {
                int index = (Integer) listPool.get(flag);
                pool.play(index, 1, 1, 0, 0, 1);
            }
        }else{
            System.out.println("===========语音提示关闭");
        }
    }

}
